ドレミファソラシド/processing

sine waveを利用してドレミの音を出す。テストを兼ねて投稿してみます!

sine waveを利用してドレミの音を出す。テストを兼ねて投稿してみます!

import krister.Ess.*;

int doremi = 8;
AudioChannel[] sound = new AudioChannel[doremi];
SineWave[] sine = new SineWave[doremi];
int x = 0;

void setup(){
  size(150,150);
  frameRate(30); 
  Ess.start(this);
  
  for(int i=0; i<doremi; i++){
    sound[i] = new AudioChannel();
    sound[i].initChannel(sound[i].frames(100));
    sine[i] = new SineWave(800,0.1);
    sine[i].generate(sound[i]);
  }
}

void draw(){
  background(255-x);
}

void keyPressed(){
  x = x +3;
  
  if(key == 'a' ){
    sine[0].frequency = 523;
    sine[0].generate(sound[0]);
    sound[0].play();
  }

   if(key == 's' ){
    sine[1].frequency = 587;
    sine[1].generate(sound[1]);
    sound[1].play();
  }

   if(key == 'd' ){
    sine[2].frequency = 659;
    sine[2].generate(sound[2]);
    sound[2].play();
  }

   if(key == 'f' ){
    sine[3].frequency = 698;
    sine[3].generate(sound[3]);
    sound[3].play();
  }

   if(key == 'j' ){
    sine[4].frequency = 784;
    sine[4].generate(sound[4]);
    sound[4].play();
  }

   if(key == 'k' ){
    sine[5].frequency = 880;
    sine[5].generate(sound[5]);
    sound[5].play();
  }

   if(key == 'l' ){
    sine[6].frequency = 988;
    sine[6].generate(sound[6]);
    sound[6].play();
  }

   if(key == ';' ){
    sine[0].frequency = 1047;
    sine[0].generate(sound[0]);
    sound[0].play();
  }

}